package cn.com.egova.mahjong.db;

import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

public class DBHelper {
    private static final String DB_PATH = Environment.getExternalStorageDirectory() + "/mahjong/database.db";
    private static SQLiteDatabase database = null;

    /**
     * 取得可读写数据库实例.
     *
     * @return 数据库
     */
    public synchronized static SQLiteDatabase getWritableDatabase() {
        if (database == null || !database.isOpen()) {
            database = SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READWRITE);
        }
        return database;
    }


    /**
     * 取得只读数据库实例.
     *
     * @return 数据库
     */
    public synchronized static SQLiteDatabase getReadableDatabase() {
        if (database == null || !database.isOpen()) {
            database = SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READONLY);
        }
        return database;
    }

    public static synchronized void closeDatabase() {
        if (database != null && database.isOpen()) {
            database.close();
            database = null;
        }
    }
}
